package com.tourism.storage.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.t2cn.common.dbwrap.DbWrapException;
import com.t2cn.common.dbwrap.Jdbc;
import com.tourism.storage.ConnextionProxy;
import com.tourism.storage.JdbcUtil;
import com.tourism.storage.dto.PassengersDto;
import com.tourism.storage.dto.TeamsDto;
import com.tourism.storage.pojo.Passengers;
import com.tourism.storage.pojo.Teams;

public class PassengersDao {
	public int addPassengers(Passengers pojo){
		Connection conn = null;
		try {
			conn = ConnextionProxy.getTestConnection();
			int i = Jdbc.Insert.insert(conn, pojo);
			conn.commit();
			return i;
		} catch (DbWrapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			Jdbc.Resource.closeConnection(conn);
		}
		return 0;
	}
	public int updatePassengers(Passengers pojo){
		Connection conn = null;
		try {
			conn = ConnextionProxy.getTestConnection();
			Passengers where = new Passengers();
			where.setInfoid(pojo.getInfoid());
			int i = Jdbc.Update.update(conn, pojo, where);
			conn.commit();
			return i;
		} catch (DbWrapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			Jdbc.Resource.closeConnection(conn);
		}
		return 0;
	}
	public List<Passengers> getAllPassengerses(Passengers pojo){
		Connection conn = null;
		try {
			conn = ConnextionProxy.getTestConnection();
			String sql = "select infoid, updatetime, teamid, adults, childs,p.courseid," +
			"departuredate,contact, p.phone,fax,p.price,paymode,paid,p.ifrom, " +
			"p.status from passengers p "; //+
//			"where p.status = " + 1;
			System.out.println("-=-=-=-=-=-=-=-=-=getPassengerses-=-=-=-=-=-=-=-=-=");				
			List<Passengers> list = Jdbc.Select.select(conn, pojo,sql,Jdbc.NULL_LIST);
			return list;
		} catch (DbWrapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			Jdbc.Resource.closeConnection(conn);
		}
		return null;
	}
	
	public List<Passengers> getTeamPassengerses(Passengers pojo){
		Connection conn = null;
		try {
			conn = ConnextionProxy.getTestConnection();
			String sql = "select infoid, updatetime, teamid, adults, childs,p.courseid," +
			"departuredate,contact, p.phone,fax,p.price,paymode,paid,p.ifrom, " +
			"p.status from passengers p " +
			"where p.teamid = " + pojo.getTeamid();
			System.out.println("-=-=-=-=-=-=-=-=-=getTeamPassengerses-=-=-=-=-=-=-=-=-=");				
			List<Passengers> list = Jdbc.Select.select(conn, pojo,sql,Jdbc.NULL_LIST);
			return list;
		} catch (DbWrapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			Jdbc.Resource.closeConnection(conn);
		}
		return null;
	}
	
	public List<PassengersDto> getTeamPassengersesDto(Passengers pojo){
	
		//List<Passengers> list = getTeamPassengerses(pojo);
		List<Passengers> list = getPassengerses(pojo);
		List<PassengersDto> dto_list = new ArrayList<PassengersDto>();
		
		for(int i=0; i < list.size(); i++){
			PassengersDto dto = new PassengersDto();
			dto = Pojo2Dto(list.get(i));
			dto_list.add(dto);
		}
		return dto_list;
	}
	
	private PassengersDto Pojo2Dto(Passengers pojo){
		PassengersDto dto = new PassengersDto();
		dto.setPassengers(pojo);

		CourseDao courseDao = new CourseDao();
		dto.setCoursename(courseDao.getCourseById(pojo.getCourseid()).getCoursename());
		
		PartnerDao passengersDao = new PartnerDao();
		dto.setPartnername(passengersDao.getPartberById(pojo.getIfrom()).getPartnername());
		
		return dto;	
	}	
	
	public List<Passengers> getPassengerses(Passengers pojo){
		Connection conn = null;
		try {
			conn = ConnextionProxy.getTestConnection();	
			List<Passengers> list = Jdbc.Select.select(conn, pojo);
			return list;
		} catch (DbWrapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			Jdbc.Resource.closeConnection(conn);
		}
		return null;
	}
	
	
	
//	public List<PassengersDto> getPassengersDtos(Passengers pojo){
//		Connection conn = null;
//		try {
//			conn = ConnextionProxy.getTestConnection();
//			List params = new ArrayList();
//			
//			String sql = "select infoid, updatetime, teamid, adults, childs,p.courseid,c.coursename " +
//			"departuredate,contact, p.phone,fax,p.price,paymode,paid,p.ifrom,pt.partnername, " +
//			"p.status from passengers p, courses c, partner pt " +
//			"where c.courseid = p.courseid and pt.partnerid = p.ifrom and p.status = ?";
//			
//			List<PassengersDto> list = Jdbc.Select.select(conn, new PassengersDto(), sql+whereSQL, params);
//			return list;
//		} catch (DbWrapException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} finally{
//			Jdbc.Resource.closeConnection(conn);
//		}
//		return null;
//	}
	public Passengers getPassengersById(Integer infoid){
		Connection conn = null;
		try {
			conn = ConnextionProxy.getTestConnection();
			Passengers pojo = new Passengers();
			pojo.setInfoid(infoid);
			List<Passengers> list = Jdbc.Select.select(conn, pojo);
			return (Passengers)Jdbc.Select.getFirstNode(list);
		} catch (DbWrapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			Jdbc.Resource.closeConnection(conn);
		}
		return null;
	}
	
	public int deletePassengers(Integer infoid){
		Connection conn = null;
		try {
			conn = ConnextionProxy.getTestConnection();
			Passengers where = new Passengers();
			where.setInfoid(infoid);
			int i = Jdbc.Delete.delete(conn, where);
			conn.commit();
			return i;
		} catch (DbWrapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			Jdbc.Resource.closeConnection(conn);
		}
		return 0;
	}

	public int getTeamPassengers(Integer teamid){
		Connection conn = null;
		try {
			conn = ConnextionProxy.getTestConnection();
			String sql = "select sum(adults)+sum(childs) from passengers where teamid =" + teamid; //+

			System.out.println("-=-=-=-=-=-=-=-=-=getTeamPassengers-=-=-=-=-=-=-=-=-=");	
			int sum = 0;
			sum = (int)Jdbc.Pagination.count(conn, sql, Jdbc.NULL_LIST);
			return sum;
		} catch (DbWrapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			Jdbc.Resource.closeConnection(conn);
		}
		
		return -1;
	}
}
